﻿@{
    //ViewBag.Title = "Danh mục nhóm đơn vị";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";

    SelectList slState = new SelectList(new[] {
        new SelectListItem { Value = "2", Text = "Tất cả"},
        new SelectListItem { Value = "0", Text = "Nhóm khác"},
        new SelectListItem { Value = "1", Text = "Nhóm cơ bản"}
    }, "Value", "Text", 2);
}
<div class="page-bar">
    <ul class="page-breadcrumb" style="padding: 4px;">
        <li>
            <i class="fa fa-home"></i>
            @*<a href="#">Quản lý danh mục nhóm đơn vị</a>*@
            <a href="#">@ViewBag.CurrentMenuName</a>
        </li>
    </ul>
</div>
<div class="row">
    <div class="col-md-2 col-sm-2" style="">@Html.Label("Tên nhóm đơn vị: ", new { id = "lblName", @class = "" })</div>
    <div class="col-md-3 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("ten_nhom_don_vi", "", new { @class = "form-control input-sm", style = "", placeholder = "Tên nhóm đơn vị", maxlength = 255 })</div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Loại nhóm")</div>
    <div class="col-md-3 col-sm-4" style="margin-bottom: 10px;">
    @Html.DropDownList("loai_nhom", slState , null, new { @class = "form-control input-sm" })</div>

    <div class="col-md-6 col-sm-6">
        <div class="pull-right">
            <button id="btnSearch" class="btn btn-sm btn-default"><i class="fa fa-search"></i> Tìm kiếm</button>
            <button id="btnAdd" class="btn btn-sm btn-default" style="cursor:pointer;"><i class="fa fa-plus-square-o"></i> Tạo mới</button>            
            @*<button class="btn btn-sm btn-default" onclick="onCreate()" style="cursor:pointer;"><i class="fa fa-plus-square-o"></i> Tạo mới</button>*@            
            <div id="divPrintEnum" style="display:none;"></div>
        </div>
    </div>
</div>

<div id="gridContent" align="center" style="padding:10px;">
    @if (Model.Data != null && Model.Data.Count > 0)
    {
       Html.RenderPartial("_NhomDonViList");
    }
    else
    {
        <h4>Không tìm thấy kết quả nào phù hợp</h4>
    }
</div>


<script type="text/javascript">
    $(document).ready(function () {
   
        $('#btnSearch').bind('click', function (event) {
            btnSearchClick();
        });

        $('#btnAdd').bind('click', function (event) {
            functionCreateOrUpdate("", "Thêm mới", "Tạo mới");
        });

        $('#input-state').change(function () {
            OnCriteria(0);
        });

        $("#input-code", "#input-name").on("keypress", function (event) {
            if (event.which == 13) {
                OnCriteria(0);
            }
        });
        headers['VerificationToken'] = '@GetAntiForgeryToken()';
    });

    var headers = {};

    function btnSearchClick() {
        //alert("search");
        OnCriteria(0);
    }

    function nhomdonviEdit(id) {
        functionCreateOrUpdate(id, "Cập nhật", "Cập nhật");
    }

    function functionCreateOrUpdate(id, titleForm, buttonTile) {
        $("#divLoading").show();        
        var ajax = $.ajax({
            url: '/NhomDonVi/CreatePopupUpdate',
            type: 'POST',
            data: { id: id },
            //contentType: "appliction/json",
            success: function (data) {
                $("#divLoading").hide();
                var box = bootbox.dialog({
                    title: titleForm,
                    message: data,
                    onEscape: function () {
                        box.modal('hide');
                    },
                    buttons: {
                        success: {
                            label: "<i class='fa fa-plus-square-o'></i> " + buttonTile,
                            className: "btn btn-md btn-success",
                            callback: function () {
                                var form = $('#frmNhomDonVi');                                
                                if (!form.valid()) {                                  
                                    return false;
                                }
                                //var strIsCoBan = "0";
                                //if ($("#BOOL_CO_BAN").prop('checked') == true) {
                                //    strIsCoBan = "1";
                                //}
                                //else {
                                //    strIsCoBan = "0";
                                //}
                                $("#divLoading").show();
                                //alert("headers");
                                $.ajax({
                                    url: '/NhomDonVi/ActionSave',
                                    headers: headers,
                                    data: {
                                        NhomDonViId: $("#NhomDonViId").val(),
                                        TenNhomDonVi: $("#TenNhomDonVi").val(),
                                        BOOL_CO_BAN: $("#BOOL_CO_BAN").prop('checked'),
                                        GhiChu: $("#GhiChu").val()
                                    },
                                    dataType: 'json',
                                    type: 'post',
                                    success: function (response) {
                                        if (response != null && response.status == 200) {
                                            Layout.toastr("",response.result);
                                            OnCriteria(0);
                                            result = true;
                                        } else {
                                            Layout.toastr("", response.result);
                                        }
                                        $("#divLoading").hide();
                                    },                                                                       
                                    error: function (errorThrown) {//function(request, textStatus, errorThrown)  {
                                        //alert(JSON.stringify(errorThrown));
                                        //alert(request.getAllResponseHeaders());
                                        //alert(request.getResponseHeader('WWW-Authenticate'));                                  
                                    }
                                });

                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-md btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            },
            complete: function () {
                setTimeout(function () {
                    $("#FULL_NAME").focus();
                }, 500);
            },
            error: function (ex) {
                //alert(JSON.stringify(ex));
            }
        });
        return false;
    }

    @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + "," + formToken;
        }
    }
</script>